package com.mxchip.livestarmobile.mobile.ui.recent

import android.content.Context
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.mxchip.livestarmobile.R
import com.mxchip.livestarmobile.mobile.db.entity.CallLogWithContact
import com.mxchip.livestarmobile.mobile.ui.ItemHolder
import kotlinx.android.synthetic.main.list_item_calllog.*
import kotlinx.android.synthetic.main.list_item_contact.text_name

class RecentRecyclerAdapter(private val onClickListener: ((View, CallLogWithContact) -> Unit)? = null) : RecyclerView.Adapter<ItemHolder>() {

    var data: List<CallLogWithContact> = emptyList()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemHolder {
        val view = LayoutInflater.from(parent.context)
                .inflate(R.layout.list_item_calllog, parent, false)
        return ItemHolder(view)
    }

    override fun getItemCount(): Int {
        return data.size
    }

    override fun onBindViewHolder(holder: ItemHolder, position: Int) {

        val item = data[position]

        if (item.contact.isNotEmpty()) {
            holder.text_full_name.visibility = View.VISIBLE
            holder.text_full_name.text = item.contact[0]
        } else {
            holder.text_full_name.visibility = View.GONE
        }
        holder.text_name.text = item.callLog.number
        holder.text_sub.text = item.callLog.startTime.toString()

        if (item.callLog.type == "2") {
            holder.icon_type.visibility = View.VISIBLE
        } else {
            holder.icon_type.visibility = View.GONE
        }

        holder.containerView.setOnClickListener {
            onClickListener?.invoke(it, data[position])
        }
    }

  }